//单设备自动化识别设备参数版本    调试版本
package org.test.springbootdisplaypage.device;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;

//@Component
public class Phione {
    private URL url = null;
    private DesiredCapabilities caps = null;
    public AppiumDriver driver;
    public WebDriverWait wait;

    //显式等待时间
    private int EXPLICITTIME = 30;

    public Phione() {
        try {
            url = new URL("http://localhost:4723");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        // 获取设备信息
        String udid = getDeviceUdid();
        String platformVersion = getPlatformVersion();
        String deviceName = getDeviceName();

        // 调试输出设备信息
        System.out.println("Device UDID: " + udid);
        System.out.println("Platform Version: " + platformVersion);
        System.out.println("Device Name: " + deviceName);

        // 配置 iOS 设备和系统设置应用程序
        caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("deviceName", deviceName); // 自动获取设备名称
        caps.setCapability("platformVersion", platformVersion);
        caps.setCapability("automationName", "XCUITest");
        caps.setCapability("udid", udid);
        caps.setCapability("bundleId", "com.zebra.messenger");
        caps.setCapability("usePrebuiltWDA", true);
        caps.setCapability("noReset", true);

        // 创建驱动对象
        driver = new IOSDriver(url, caps);

        // 创建 WebDriverWait 对象，并指定等待时间显式等待时间
        wait = new WebDriverWait(driver, Duration.ofSeconds(EXPLICITTIME));
    }

    //创建一个shell类用来获取udid和设备名称和当前系统版本
    private String runCommand(String command) {
        StringBuilder output = new StringBuilder();
        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line);
            }
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return output.toString().trim();
    }

    private String getDeviceUdid() {
        return runCommand("idevice_id -l");
    }

    private String getPlatformVersion() {
        return runCommand("ideviceinfo -k ProductVersion");
    }

    private String getDeviceName() {
        return runCommand("ideviceinfo -k DeviceName");
    }
}











//
////手动输入的版本
//package org.test.springbootdisplaypage.device;
//
//import io.appium.java_client.AppiumDriver;
//import io.appium.java_client.ios.IOSDriver;
//
//import org.openqa.selenium.remote.DesiredCapabilities;
//import org.openqa.selenium.support.ui.WebDriverWait;
//
//import java.net.MalformedURLException;
//import java.net.URL;
//import java.time.Duration;
//
//public class  Phione {
//    private   URL url = null;
//    private   DesiredCapabilities caps = null;
//    public  AppiumDriver driver;
//    public  WebDriverWait wait;
//    //显式等待时间
//    private  int EXPLICITTIME=30;
//
//    public Phione(){
//        try {
//            url = new URL("http://localhost:4723");
//        } catch (MalformedURLException e) {
//            throw new RuntimeException(e);
//        }
//
//        // 配置 iOS 设备和系统设置应用程序
////        caps = new DesiredCapabilities();
////        caps.setCapability("platformName", "iOS");
////        caps.setCapability("deviceName", "iPhone Xs Max");
////        caps.setCapability("platformVersion", "16.0");//本机的
////        caps.setCapability("automationName","XCUITest");
////        caps.setCapability("udid", "00008020-000E5D363A68002E");//本机的
////        caps.setCapability("bundleId", "com.zebra.messenger"); // 替换为你要测试的应用程序的 bundle identifier
//////            caps.setCapability("noReset", true);//这个的意思是关闭驱动程序以后保留之前的数据
////        caps.setCapability("usePrebuiltWDA", true);// 替换为你要测试的应用程序的 bundle identifier
////        caps.setCapability("noReset", true);//这个的意思是关闭驱动程序以后保留之前的数据
//
//
////        // 配置 iOS 设备和系统设置应用程序
//        caps = new DesiredCapabilities();
//        caps.setCapability("platformName", "iOS");
//        caps.setCapability("deviceName", "iPhone 11");
//
//        caps.setCapability("platformVersion", "15.7");
//        caps.setCapability("automationName","xcuitest");
//        caps.setCapability("udid", "00008030-00062D5C2260402E");
//        caps.setCapability("bundleId", "com.zebra.messenger");
//        caps.setCapability("noReset", true);//这个的意思是关闭驱动程序以后保留之前的数据
//        caps.setCapability("usePrebuiltWDA", true);// 替换为你要测试的应用程序的 bundle identifier
//        caps.setCapability("noReset", true);//这个的意思是关闭驱动程序以后保留之前的数据
//
//
//        // 创建驱动对象
//        driver = new IOSDriver(url, caps);
//
//        // 创建 WebDriverWait 对象，并指定等待时间显式等待时间
//        wait = new WebDriverWait(driver, Duration.ofSeconds(EXPLICITTIME));
//    }
//}





//单设备自动获取参数的版本
//
//package org.test.springbootdisplaypage.device;
//
//import io.appium.java_client.AppiumDriver;
//import io.appium.java_client.ios.IOSDriver;
//import org.openqa.selenium.remote.DesiredCapabilities;
//import org.openqa.selenium.support.ui.WebDriverWait;
//import org.springframework.stereotype.Component;
//
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.net.MalformedURLException;
//import java.net.URL;
//import java.time.Duration;
//
//@Component
//public class Phione {
//    private URL url = null;
//    private DesiredCapabilities caps = null;
//    public AppiumDriver driver;
//    public WebDriverWait wait;
//
//    //显式等待时间
//    private int EXPLICITTIME = 30;
//
//    public Phione() {
//        try {
//            url = new URL("http://localhost:4723");
//        } catch (MalformedURLException e) {
//            throw new RuntimeException(e);
//        }
//
//        // 获取设备信息
//        String udid = getDeviceUdid();
//        String platformVersion = getPlatformVersion();
//        String deviceName = getDeviceName();
//
//        // 配置 iOS 设备和系统设置应用程序
//        caps = new DesiredCapabilities();
//        caps.setCapability("platformName", "iOS");
//        caps.setCapability("deviceName", deviceName); // 自动获取设备名称
//        caps.setCapability("platformVersion", platformVersion);
//        caps.setCapability("automationName", "XCUITest");
//        caps.setCapability("udid", udid);
//        caps.setCapability("bundleId", "com.zebra.messenger");
//        caps.setCapability("usePrebuiltWDA", true);
//        caps.setCapability("noReset", true);
//
//        // 创建驱动对象
//        driver = new IOSDriver(url, caps);
//
//        // 创建 WebDriverWait 对象，并指定等待时间显式等待时间
//        wait = new WebDriverWait(driver, Duration.ofSeconds(EXPLICITTIME));
//    }
//
//    //创建一个shell类用来获取udid和设备名称和当前系统版本
//    private String runCommand(String command) {
//        StringBuilder output = new StringBuilder();
//        try {
//            Process process = Runtime.getRuntime().exec(command);
//            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
//            String line;
//            while ((line = reader.readLine()) != null) {
//                output.append(line);
//            }
//            process.waitFor();
//        } catch (IOException | InterruptedException e) {
//            e.printStackTrace();
//        }
//        return output.toString().trim();
//    }
//
//    private String getDeviceUdid() {
//        return runCommand("idevice_id -l");
//    }
//
//    private String getPlatformVersion() {
//        return runCommand("ideviceinfo -k ProductVersion");
//    }
//
//    private String getDeviceName() {
//        return runCommand("ideviceinfo -k DeviceName");
//    }
//}